<!--
 * @Author: wuyue
 * @Date: 2023-03-06 16:24:10
 * @LastEditTime: 2023-03-07 17:14:26
 * @LastEditors: wuyue
 * @Descripttion: 计量设备发放
 * @version: 
-->
<template>
  <base-breadcrumb :title="$route.meta.title">
    <a-row :gutter="10" class="page-height">
      <a-col :span="4">
        <a-card class="page-height-tree tree-container">
          <!-- 项目列表 -->
          <PermissionTreeOrganization :showPermission="false" @select="handleSelect" />
        </a-card>
      </a-col>
      <a-col :span="20" class="page-height-list-wrapper">
        <searchData :search-data="searchArr" @search="search" />
        <a-card :bordered="false" class="page-height-list">
          <list-table
            ref="table"
            class="export-table"
            :loading="loading"
            :columns="columns"
            :outSelectedRowKeys.sync="selectedRowKeys"
            :request="loadData"
            :statistics-config="{ use: false }"
            :start-index="0"
            :export-config="{ use: true }"
            :export-config-text="{ select: '导出选中', all: '导出全部' }"
            :exportPermission="{ select: 'qm_065_aexp_03', all: 'qm_065_sexp_02' }"
            @exportSelect="(data)=>exportFile('checked', '计量设备发放管理-导出选中', data)"
            @exportAll="exportFile('all', '计量设备发放管理-导出全部')"
          >
            <span slot="serial" slot-scope="text, record, index">{{ index + 1 }}</span>
            <div slot="action" slot-scope="text, record">
              <MoreButton>
                <a v-btn-permission="'qm_065_loo_01'" @click="handleInfo(record)">查看</a>
              </MoreButton>
            </div>
          </list-table>
        </a-card>
      </a-col>
    </a-row>
  </base-breadcrumb>
</template>

<script>
import PermissionTreeOrganization from '@/components/businessComponents/PermissionTreeOrganization'
import { getList, exportFile} from '@/api/quality/meterageEquipmentGrant'
import BasePage from '@/views/basePage'
import handleDownload from '@/views/quality/equipmentManagement/constant/downloadFile.js'
import moment from 'moment';

export default {
  extends: new BasePage(),
  name: 'a' + Date.now(),
  components: {
    PermissionTreeOrganization
  },

  data() {
    return {
      selectedKeys: [], // 选中节点
      selectOptions: {}, // 选中节点对象
      queryParams: {},
      loading: false,
      columns: [
        {
          title: '序号',
          width: '65px',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '设备编号',
          dataIndex: 'mdCode'
        },
        {
          title: '设备名称',
          dataIndex: 'mdName'
        },
        {
          title: '规格型号',
          dataIndex: 'specificationModel'
        },
        {
          title: '领用单位',
          dataIndex: 'receiverCompany'
        },
        {
          title: '领用人',
          dataIndex: 'receiver'
        },
        {
          title: '领用日期',
          dataIndex: 'receiveTime',
        //   customRender: (text) => {
        //     return text&&moment(text).locale('zh-cn').format('YYYY-MM-DD')
        //   }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      loadData: parameter => {
        let { pageNo, pageSize } = parameter
        let params = {
          pageNum: pageNo,
          pageSize
        }
        this.loading = true
        const requestParameters = Object.assign({}, params, { param: this.setQueryParam() })
        return getList(requestParameters)
          .then(res => {
            this.loading = false
            if (res.code === '0') {
              return { code: 200, data: res.data?.list, totalSize: res.data?.total }
            }
          })
          .catch(err => {
            this.loading = false
          })
      },
      searchArr: [
        // 搜索框
        {
          name: '设备名称',
          value: 'mdName',
          type: 'input'
        },
        {
          name: '规格型号',
          value: 'specificationModel',
          type: 'input'
        },
        {
          name: '领用单位',
          value: 'receiverCompany',
          type: 'input'
        },
        {
          name: '设备编号',
          value: 'mdCode',
          type: 'input'
        },
        {
          name: '领用人',
          value: 'receiver',
          type: 'input'
        },
        {
          name: '领用时间',
          value: 'receiveTime',
          type: 'dateRange'
        },
      ],
      selectedRowKeys: [],
      statistics: {},
    }
  },
  methods: {
    // 导出Excel
    exportFile(tag, name, data) {
        let params = this.setQueryParam()
        if (tag === 'checked') {
            const transferIdList = data.map(item => item.grantRecordId)
            let params = this.setQueryParam()
            params.ids = transferIdList
        } else {
            delete params.ids
        }
        handleDownload(exportFile, params, name)
    },
    // 设置查询参数
    setQueryParam() {
      if (this.queryParams.receiveTime && this.queryParams.receiveTime.length > 0) {
        this.queryParams.receiveStartTime = this.queryParams.receiveTime[0].format('YYYY-MM-DD') + ' 00:00:00'
        this.queryParams.receiveEndTime = this.queryParams.receiveTime[1].format('YYYY-MM-DD') + ' 23:59:59'
        delete this.queryParams.receiveTime
      }
      const searchQuery = {}
      if (this.selectOptions.projectId) {
          searchQuery.projectSet = this.selectOptions.projectId  ? [this.selectOptions.projectId] : []
      } else if (this.selectOptions.orgId) {
          searchQuery.orgSet = [this.selectOptions.orgId]
      } else {
          delete searchQuery.orgSet
          delete searchQuery.projectSet
      }
      this.queryParams.searchQuery = searchQuery
      return  this.queryParams
    },
    handleSelect(node) {
      this.selectOptions = node
      this.search()
    },
    search(values) {
      this.queryParams = values || {}
      this.$refs.table.refresh(true)
    },
    // 详情
    handleInfo(row) {
      this.$router.push({
        path: '/quality/equipmentManagement/meterageEquipmentGrantInfo',
        query: {
         id: row.grantRecordId,   
        }
      })
    },
  }
}
</script>
